package com.bfmet.crawler;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;

import com.bfmet.common.DAOService;
import com.bfmet.common.base.EntityBase;
import com.bfmet.entity.Price;

public class PriceSpider extends Spider {

	@Override
	public void run() {
		this.getPrice("http://www.smm.cn");
	}

	private void getPrice(String url) {
		log("Fetch price begin..." + url);
		try {
			Parser p = new Parser(url);
			NodeFilter ulf = new TagNameFilter("ul");
			NodeFilter mainNewsf = new HasAttributeFilter("class", "price_n1");

			NodeList ulList = p.parse(new AndFilter(ulf, mainNewsf));
			for (int i = 0; i < ulList.size(); i++) {
				Price price = new Price();
				Node ul = ulList.elementAt(i);
				String name = ul.getChildren().elementAt(0).getNextSibling()
						.toPlainTextString();
				this.log("name:" + name);
				price.setName(name);
				String range = ul.getChildren().elementAt(2).getNextSibling()
						.toPlainTextString();
				this.log("range:" + range);
				price.setRange(range);
				String average = ul.getChildren().elementAt(4).getNextSibling()
						.toPlainTextString();
				this.log("average:" + average);
				price.setAverage(average);
				String change = ul.getChildren().elementAt(6).getNextSibling()
						.toPlainTextString();
				this.log("change:" + change);
				price.setChange(change);
				String dateStr = ul.getChildren().elementAt(8).getNextSibling()
						.toPlainTextString();
				price.setCreateTime(parseDate(dateStr));
				this.save(price);
			}
		} catch (Exception ex) {
			this.logException(ex);
		}
		log("Fetch price end..." + url);
	}

	@Override
	public boolean exist(EntityBase entity) {
		EntityManager em = DAOService.getEM();
		try {
			Query query = em.createQuery("select n from "
					+ entity.getClass().getSimpleName()
					+ " n where n.name=:name and n.createTime = :createTime");
			query.setParameter("name", entity.getName());
			query.setParameter("createTime", entity.getCreateTime());
			return !query.getResultList().isEmpty();
		} finally {
			em.close();
		}
	}

	protected Date parseDate(String value) {
		Date date = new Date();
		try {
			if (value.indexOf("-") != -1) {
				String yyyy = new SimpleDateFormat("yyyy").format(date);
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				date = format.parse(yyyy + "-" + value);
			} else if (value.indexOf(":") != -1) {
				String yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd")
						.format(date);
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm");
				date = format.parse(yyyyMMdd + " " + value);
			}
		} catch (Exception ex) {
			this.logException(ex);
		}
		return date;
	}

	public static void main(String[] arg) {
		PriceSpider ps = new PriceSpider();
		ps.run();
	}
}
